package com.huaruan.youdi.views.activitys;


import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.huaruan.youdi.R;
import com.huaruan.youdi.http.okhttp.Constant;
import com.huaruan.youdi.http.okhttp.HttpCallback;
import com.huaruan.youdi.http.okhttp.HttpUtils;
import com.huaruan.youdi.utils.AppUtils;
import com.huaruan.youdi.utils.PreferenceUtil;
import com.huaruan.youdi.utils.StringUtils;
import com.huaruan.youdi.utils.UserUtils;

import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;

public class ModifyPasswordActivity extends BaseActivity {
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.iv_password_visible)
    ImageView iv_password_visible;
    @BindView(R.id.tv_update)
    TextView tv_update;

    private boolean isPasswordVisible;
    private String userPhone;
    private String userPassword;

    @Override
    public int IGetContentViewResId() {
        return R.layout.activity_modify_password;
    }

    @Override
    public void IFindViews() {
        userPhone = PreferenceUtil.getString(PreferenceUtil.PreferenceKeys.PHONE, "");
        tv_phone.setText(userPhone);
        iv_password_visible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPasswordVisible();
            }
        });
        tv_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updatePwd();
            }
        });
    }

    @Override
    public void IInitData() {
        setPasswordVisible();
    }


    private void setPasswordVisible() {
        isPasswordVisible = !isPasswordVisible;
        if (isPasswordVisible) {
            iv_password_visible.setImageResource(R.mipmap.icon_password_visible);
            et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            iv_password_visible.setImageResource(R.mipmap.icon_password_invisible);
            et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    private void updatePwd() {
        userPassword = et_password.getText().toString();
        if (StringUtils.isEmpty(userPhone)) {
            AppUtils.toastShort(IGetContext(), "手机号码为空");
            return;
        }
        if (StringUtils.isEmpty(userPassword)) {
            AppUtils.toastShort(IGetContext(), "请输入您的密码");
            return;
        }

        SortedMap<String, String> mSortedMap = new TreeMap<>();
        mSortedMap.put("id", UserUtils.getUid());
        mSortedMap.put("phone", userPhone);
        mSortedMap.put("password", userPassword);
        new HttpUtils.Builder(IGetContext())
                .postMethod()
                .url(Constant.UPDATE_PASSWORD)
                .params(mSortedMap)
                .callback(new HttpCallback() {
                    @Override
                    public void success(Object object) {
                        AppUtils.toastShort(IGetContext(), "修改密码成功");
                        finish();
                    }
                }).start();
    }

}
